//
//  FLLSelectionTableViewCell.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/31.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLSelectionTableViewCell.h"
#import "FLLMacro.h"
#import "FLLSelectionCollectionViewCell.h"
#import "UIImageView+WebCache.h"

@interface FLLSelectionTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end
@implementation FLLSelectionTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    self.selectionCollectionView.delegate = self;
    self.selectionCollectionView.dataSource = self;
    self.selectionLayout.minimumInteritemSpacing = CellMargin;
    self.selectionLayout.minimumLineSpacing = CellMargin;
    self.selectionLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.selectionCollectionView.backgroundColor = [UIColor whiteColor];
    _selectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.selectionCollectionView registerNib:[UINib nibWithNibName:@"FLLSelectionCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"selectionCollectionCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    if (_selectionArray != 0) {
        return 3;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FLLSelectionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"selectionCollectionCell" forIndexPath:indexPath];
    
    // 下面图片
    NSMutableArray *picArray = [NSMutableArray array];
    for (NSDictionary *dic in _selectionArray) {
        if ([[dic valueForKey:@"type"] isEqualToString:@"image"] && [[dic valueForKey:@"id_Data"] integerValue] % 3 == 1) {
            NSMutableDictionary *picDic = [NSMutableDictionary dictionary];
            [picDic setValue:[dic valueForKey:@"content"] forKey:@"image"];
            [picArray addObject:picDic];
        }
    }
    [cell.selectionImageView sd_setImageWithURL:[NSURL URLWithString:picArray[indexPath.row][@"image"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
    }];
    
    // 用户头像
    NSMutableArray *userPicArray = [NSMutableArray array];
    for (NSDictionary *dic in _selectionArray) {
        if ([[dic valueForKey:@"type"] isEqualToString:@"image"] && [[dic valueForKey:@"id_Data"] integerValue] % 3 == 2 ) {
            NSMutableDictionary *userPicDic = [NSMutableDictionary dictionary];
            [userPicDic setValue:[dic valueForKey:@"content"] forKey:@"userPic"];
            [userPicArray addObject:userPicDic];
        }
    }
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:userPicArray[indexPath.row][@"userPic"]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
    }];
    
    // 用户名字
    
    NSMutableArray *userNameArray = [NSMutableArray array];
    for (NSDictionary *dic in _selectionArray) {
        if ([[dic valueForKey:@"type"] isEqualToString:@"text"] ) {
            NSMutableDictionary *userNameDic = [NSMutableDictionary dictionary];
            [userNameDic setValue:[dic valueForKey:@"content"] forKey:@"name"];
            [userNameArray addObject:userNameDic];
        }
    }
    cell.userNameLabel.text = userNameArray[indexPath.row][@"name"];
    cell.userNameLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];

        return cell;
}
// 给Item确定大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = (_selectionCollectionView.bounds.size.width - 2 * CellMargin) / 3.0;
    CGFloat itemHeight = _selectionCollectionView.bounds.size.height;
    return CGSizeMake(itemWidth, itemHeight);
    
}
- (void)setSelectionArray:(NSArray<Widget_Data *> *)selectionArray
{

    _selectionArray = selectionArray;
    [self.selectionCollectionView reloadData];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
